﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Boomerang.Common.Configuration;
using Boomerang.Common.Crm;

namespace Boomerang.Management.UIForms
{
    public partial class CrmCustomizationsForm : Boomerang.Management.UIForms.WizardFormBase
    {
        public CrmCustomizationsForm()
        {
            InitializeComponent();
        }

        public CrmCustomizationsForm(string title, WizardFormType type, Organization org)
            : base(title, type,org)
        {
            InitializeComponent();

            labelStepTitle.Text = "Configure CRM Customizations";
            labelSubTitle.Text = "Use the form below to provide the details the customizations for your CRM Organization";
        }

        private void checkBoxDeployCustomizations_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBoxCustomizationOptions.Enabled = checkBoxDeployCustomizations.Checked;
        }

        protected override bool InternalButtonClick(WizardButtonType wizardButtonType)
        {
            if (wizardButtonType == WizardButtonType.Next || wizardButtonType == WizardButtonType.Finish)
            {
                string actionTitle = (checkBoxDeployCustomizations.Checked) ? "Importing CRM Customizations..." : "Validating CRM Customizations...";
                FormLoading progressForm = new FormLoading(actionTitle);
                progressForm.Worker.DoWork += new DoWorkEventHandler(TestConnection);
                object[] parameters = new object[]
                {
                    this.org.CrmConnectionOptions,
                    new bool[]{checkBoxDeployCustomizations.Checked,checkBoxServiceWfExample.Checked}
                };
                progressForm.Worker.RunWorkerAsync(parameters);

                progressForm.ShowDialog();

                if (progressForm.Result.Error != null)
                {
                    ExceptionBox.Show(this, progressForm.Result.Error);
                    return false;
                }
                else
                {                    
                    return true;
                }

            }

            return true;
        }

        void TestConnection(object sender, DoWorkEventArgs e)
        {
            object[] parameters = (object[])e.Argument;
            CrmConnection connection = new CrmConnection((CrmConnectionOptions)parameters[0]);

            CustomizationsManager manager = new CustomizationsManager(connection);

            bool deploy = (bool)((bool[])parameters[1])[0];
            if (deploy)
            {
                manager.ProvisionCustomizations(".\\Customizations\\BoomerangSmsEntity.xml");
                bool serviceWf = (bool)((bool[])parameters[1])[1];
                if (serviceWf) manager.ProvisionWorkflows(".\\Customizations\\BoomerangServiceWf.xml");
            }
            else
                manager.ValidateCustomizations();           
        }

    }
}
